<?php
// +----------------------------------------------------------------------
// | 文章分类控制器
// +----------------------------------------------------------------------
// | Author: Eric <ieyangxiaoguo@126.com>
// +----------------------------------------------------------------------
// | Copyright ©2017 http://www.zzuyxg.top All rights reserved.
// +----------------------------------------------------------------------
// | 2017年10月10日
// +----------------------------------------------------------------------
namespace app\tdadmin\controller;

use app\common\controller\AdminBaseController;
use app\common\model\CategoryModel;
use app\common\model\SpecialtyModel;
use think\Db;
use think\Validate;

class SpecialtyController extends AdminBaseController
{
    /**
     * 专业列表
     */
    public function index()
    {
        $this->assign('college_id', $this->request->param("college_id"));
        return $this->fetch();
    }

    /**
     * 分类列表--数据表格数据接口
     * @author Eric ieyangxiaoguo@126.com
     * @param int $trashed 默认0，过滤已删除的分类；所有分类
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public function apiGetSpecialty()
    {
        $model = new SpecialtyModel();
        $college_id = $this->request->param("college_id");
        if ($college_id) {
            $where = array(
                'college_id' => $college_id
            );
        } else {
            $where = [];
        }
        $categorys = $model->alias('a')->join('sk_college c', 'c.id = a.college_id')->where($where)->field('a.id,college_id,specialty_name,a.status,a.sort,c.college_name')->all();
        $res['code'] = 0;
        $res['msg'] = '';
        $res['data'] = $categorys;

        return $res;
    }

    /**
     * 添加专业
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $rules = [
                'specialty_name' => 'require|unique:Specialty,specialty_name',
            ];
            $messages = [
                'specialty_name.unique' => '专业已存在',
            ];
            $validate = new Validate($rules, $messages);

            if ($validate->check($data)) {
                $res['code'] = 1;
                $res['msg'] = '添加成功！';
                $model = new SpecialtyModel();
                $result = $model->addSpecialty($data);
                if ($result === false) {
                    $res['code'] = 0;
                    $res['msg'] = '添加失败！';
                }
                addLog("添加了新专业：".$data['specialty_name'],Db::getLastSql());

                return $res;
            } else {
                $res['code'] = 0;
                $res['msg'] = $validate->getError();
                return $res;
            }
        } else {
            // $categories = $this->apiGetSpecialty(1);
            // $this->assign('specialties', $categories['data']);
            return $this->fetch();
        }
    }

    /**
     * 编辑分类
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $specialty = SpecialtyModel::where('id', $data['id'])->find();
            $check = SpecialtyModel::where('college_id', $specialty['college_id'])->where('specialty_name', $data['specialty_name'])->find();
            if ($check) {
                $res['code'] = 0;
                $res['msg'] = '保存失败！';
                return $res;
            }
            $result = SpecialtyModel::where('id', $data['id'])->update(['specialty_name' => $data['specialty_name']]);
            if ($result === false) {
                $res['code'] = 0;
                $res['msg'] = '保存失败！';
            }
            $res['code'] = 1;
            $res['msg'] = '保存成功！';

            addLog("修改了专业信息：".$data['id'],Db::getLastSql());

            return $res;

        } else {
            $id = $this->request->param('id', 0, 'intval');
            if ($id > 0) {
                $category = SpecialtyModel::get($id);

                $this->assign('specialty', $category);
                return $this->fetch();
            } else {
                $res['code'] = 0;
                $res['msg'] = '你从哪里来？';
                return $res;
            }
        }
    }

    //检测分类是否重复
    public function checkName()
    {
        $name = $this->request->param('name');

        $data['code'] = 1;
        $data['msg'] = '该名称可用！';

        if (SpecialtyModel::where('specialty_name', $name)->find()) {
            $data['code'] = 0;
            $data['msg'] = '该分类已存在';
        }

        return $data;
    }

    /**
     * 显示/开启操作
     *
     * @author Eric ieyangxiaoguo@126.com
     * @date 2017年11月26日
     */
    public function doSwitch()
    {
        return parent::switchOperate(new CollegeModel());
    }

    /**
     * 更新字段
     */
    public function updateField()
    {
        return parent::fieldUpdate(new SpecialtyModel());
    }

    /**
     * 删除文章分类
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $real = $this->request->param('real');
        if ($id) {
            $findCategory = CategoryModel::withTrashed()->get($id);

            if ($real) {
                $result = $findCategory->delete(true);
                $msg = '彻底删除';
            } else {
                $result = $findCategory->delete();
                $msg = '删除';
            }

            if ($result) {
                $data['code'] = 1;
                $data['msg'] = $msg . '成功！';
            } else {
                $data['code'] = 0;
                $data['msg'] = $msg . '失败！';
            }
        } else {
            $data['code'] = 0;
            $data['msg'] = '你从哪里来？';
        }

        return $data;
    }

    /**
     * 恢复分类
     */
    public function restore()
    {
        $id = $this->request->param('id');
        if ($id) {
            $categoryModel = new CategoryModel();
            // 获取删除的内容
            $findCategory = $categoryModel->onlyTrashed()->get($id);
            $result = $findCategory->restore();

            if ($result) {
                $data['code'] = 1;
                $data['msg'] = '恢复成功！';
            } else {
                $data['code'] = 0;
                $data['msg'] = '恢复失败！';
            }
        } else {
            $data['code'] = 0;
            $data['msg'] = '你从哪里开？';
        }

        return $data;
    }

}
